/*
 * Copyright (C) 2017 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.network;

import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

import static com.network.Status.COMPLETE;
import static com.network.Status.ERROR;
import static com.network.Status.LOADING;
import static com.network.Status.SUCCESS;
import static com.network.Status.THROWABLE;

/**
 * A generic class that holds a value with its loading status.
 *
 * @param <T>
 */
public class Resource<T> {

    @NonNull
    public final Status status;

    @Nullable
    public String message;

    @Nullable
    public final T detail;

    //错误码
    public String result;

    @Nullable
    public final Throwable mException;


    public Resource(@NonNull Status status, @Nullable T detail, @Nullable String message, Throwable e) {
        this.status = status;
        this.detail = detail;
        this.message = message;
        this.mException = e;
    }

    public Resource(@NonNull Status status, @Nullable T detail, @Nullable String message, Throwable e, String result) {
        this.status = status;
        this.detail = detail;
        this.message = message;
        this.mException = e;
        this.result = result;
    }

    public static <T> Resource<T> success(@Nullable T detail) {
        return new Resource<>(SUCCESS, detail, null, null);
    }

    public static <T> Resource<T> error(String msg, @Nullable T detail, String errorCode) {
        return new Resource<>(ERROR, detail, msg, null, errorCode);
    }

    public static <T> Resource<T> loading(@Nullable T detail) {
        return new Resource<>(LOADING, detail, null, null);
    }

    public static <T> Resource<T> complete(@Nullable T detail) {
        return new Resource<>(COMPLETE, detail, null, null);
    }

    public static <T> Resource<T> throwable(@Nullable T detail, Throwable e) {
        return new Resource<>(THROWABLE, detail, null, e);
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }

        Resource<?> resource = (Resource<?>) o;

        if (status != resource.status) {
            return false;
        }
        if (message != null ? !message.equals(resource.message) : resource.message != null) {
            return false;
        }
        return detail != null ? detail.equals(resource.detail) : resource.detail == null;
    }

    @Override
    public int hashCode() {
        int result = status.hashCode();
        result = 31 * result + (message != null ? message.hashCode() : 0);
        result = 31 * result + (detail != null ? detail.hashCode() : 0);
        return result;
    }

    @Override
    public String toString() {
        return "Resource{" +
                "status=" + status +
                ", message='" + message + '\'' +
                ", data=" + detail +
                '}';
    }


    public T getResponse() {
        return detail;
    }


}
